package com.andrew.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.andrew.entity.Cart;
import com.andrew.service.impl.CartServiceImpl;
import com.sun.media.sound.RealTimeSequencerProvider;

public class AddCartSrevlet extends HttpServlet {
	private CartServiceImpl csi=new CartServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");

		String result=null;
		int bookId=Integer.parseInt(req.getParameter("bookId"));
		int quantity=Integer.parseInt(req.getParameter("quantity"));
		Cart cart=(Cart)req.getSession().getAttribute("cart");
		
		System.out.println(bookId+","+quantity+","+cart);
		
		try {
			result=csi.addCart(bookId, quantity, cart);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(result.equals("success")) {
			//加入购物车成功
			resp.getWriter().write("success");
		}else {
			resp.getWriter().write("fail");
		}
		
	}

}
